package cn.ymcc.controller;

import cn.ymcc.fegin.OtherClient;
import cn.ymcc.fegin.SearchClient;
import cn.ymcc.mq.StreamClient;
import cn.ymcc.pojo.User;
import com.netflix.discovery.EurekaClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@EnableBinding(StreamClient.class)
public class CustomerController {

    @Autowired
    private RestTemplate restTemplate;

    @Resource
    private EurekaClient eurekaClient;

    @Resource
    private SearchClient feginSearch;  //远程服务

    @Autowired
    private OtherClient otherClient;//调用第三方服务

    @Autowired
    private StreamClient streamClient;//消息队列


    @GetMapping("/customer")
    @HystrixCommand(fallbackMethod = "test01Fallback", commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),//断路器的开关，默认为开的
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"), //失败阈值的总请求数
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "70"),//达到多少百分比 ，就开始开启断路
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "5000")//开启短路时，多少秒之后才开始试探性访问
    })
    public String test01() {


        int i = 5 / 0;

        String result = feginSearch.test01();
        return result;
    }


    public String test01Fallback() {

        return "这里出错了阿西吧";
    }

    @GetMapping(value = "findById/{id}")
    @HystrixCommand(fallbackMethod = "findByIdFallback", commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),//断路器的开关，默认为开的
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"), //失败阈值的总请求数
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "70"),//达到多少百分比 ，就开始开启断路
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "5000")//开启短路时，多少秒之后才开始试探性访问
    })
    public User findById(@PathVariable(value = "id") Integer id) {

        if (id == 1) {
            int y = id / 0;
        }

        return feginSearch.findById(id);
    }

    public User findByIdFallback(@PathVariable(value = "id") Integer id) {

        return new User(id, "错误", "代码");
    }


    @PostMapping("/findAll")
    public User fidnAll(User user) {

        return feginSearch.findAll(user);
    }


    @Value("${version}")
    private String version;

    @GetMapping("/test")
    public String et() {

        return version;
    }

    @GetMapping("/other1")
    public String te() {
        return otherClient.test();
    }


    @GetMapping("/send")
    public String send() {
        streamClient.output().send(MessageBuilder.withPayload("Hello Stream!!").build());
        return "消息发送成功！！";
    }
}
